﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CWB.Common.Utilities;
using CWB.Common.Logging;
using System.Net;
using CWB.Common.Constant;
using CWB.Web.VoiceConvert;

namespace CWB.Web.Misc
{
    public class VoiceConverter
    {
        private static readonly ISysLog Log = SysLog.GetLogger(typeof(VoiceConverter));

        public static bool CreateVoiceFile(string VoiceWord, string FullFileName)
        {
            //VoiceConvert.PortTypeClient pc = new CWB.Web.VoiceConvert.PortTypeClient();
            PortTypeClient pc = new PortTypeClient();
            string convertResult = pc.ConvertSimple("roy", "f123456789", VoiceWord);
            VoiceConvertResult vcRequestResult = VoiceTools.VoiceRequestResult(convertResult);
            VoiceConvertResult vcConvertResult = new VoiceConvertResult();
            int times = 0;
            bool rtn = false;
            if (vcRequestResult.ResultCode.Trim() == "0")
            {
                System.Threading.Thread.Sleep(1000);
                while (true)
                {
                    times++;
                    convertResult = pc.GetConvertStatus("roy", "f123456789", vcRequestResult.ResultId);
                    vcConvertResult = VoiceTools.VoiceConvertResult(convertResult);
                    if (vcConvertResult.ResultCode == "0")
                    {
                        bool leave = false;
                        switch (vcConvertResult.ResultStatus)
                        {
                            case 1:
                            case 0:
                                Log.Debug(string.Format("Watting string : {0}", vcConvertResult.ResultPath));
                                int watingsecond = Convert.ToInt32(vcConvertResult.ResultPath);
                                if (watingsecond == 0)
                                {
                                    watingsecond = 1000;
                                }
                                else
                                {
                                    watingsecond *= 1000;
                                }
                                System.Threading.Thread.Sleep(watingsecond);
                                break;
                            case 2:
                                Log.Debug(string.Format("Return Url : {0}", vcConvertResult.ResultPath));
                                // Create a new WebClient instance.
                                using (WebClient ttsServer = new WebClient())
                                {
                                    ttsServer.DownloadFile(vcConvertResult.ResultPath, FullFileName);
                                }
                                leave = true;
                                rtn = true;
                                break;
                            default:
                                Exception ex = new Exception(string.Format("Unhandel Exception, Third party return status:{0}", vcConvertResult.ResultStatus));
                                Log.Exception(ex);
                                leave = true;
                                break;
                        }
                        if (leave)
                        {
                            break;
                        }
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(Convert.ToInt32(vcConvertResult.ResultPath));
                    }

                    //If the status alway can't be Zeor, then break the work.
                    #region If can't Get Result final.
                    if (times > 10)
                    {
                        Exception ex = new Exception(string.Format("Timeout, Third party return code:{0}, return string:{1}", vcConvertResult.ResultCode, vcConvertResult.ResultString));
                        Log.Exception(ex);
                        break;
                    }
                    #endregion
                }
            }
            else
            {
                Log.Debug(string.Format("Return code:{0}, Return String:{1}", vcRequestResult.ResultCode, vcRequestResult.ResultString));
                Log.Exception(Localization.TrandlateMessage(Err.ConvertVoiceFailed));
            }
            return rtn;
        }
    }
}
